package com.inspinia.base.util;


import org.springframework.beans.BeanUtils;

import java.util.Map;

/**
 * javabean相关的工具类
 * Created by veblen on 16/9/19.
 */
public class BeanUtil extends BeanUtils {

    /**
     * 将一个对象转成Map
     *
     * @param o
     * @return
     */
    public static Map<String, Object> object2Map(final Object o) {
        String s = JsonUtils.toJson(o);
        return JsonUtils.fromJson(s, Map.class);
    }

    /**
     * map转object
     * @param map
     * @param beanClass
     * @return
     * @throws Exception
     */
    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass)
            throws Exception {
        if (map == null) {
            return null;
        }
        Object obj = beanClass.newInstance();
        org.apache.commons.beanutils.BeanUtils.populate(obj, map);
        return obj;
    }

    public static Map<?, ?> objectToMap(Object obj) {
        if (obj == null) {
            return null;
        }
        return new org.apache.commons.beanutils.BeanMap(obj);
    }
}
